#!/bin/sh
# chkconfig: 2345 10 90
#服务必须在运行级2，3，4，5下被启动或关闭，启动的优先级是90，关闭的优先级是10。
# description: Start and Stop xxx
# Name: node_exporter

# Source function library.
. /etc/rc.d/init.d/functions

# Source networking configuration.
. /etc/sysconfig/network

# Check that networking is up.
[ "$NETWORKING" = "no" ] && exit 0

DAEMON_PATH="/usr/local/bin/node_exporter"
# CONF_FILE="/etc/node_exporter/config.yml"
LOCK_FILE="/var/lock/subsys/node_exporter"
prog=$(basename $DAEMON_PATH)

start() {
    [ -x $DAEMON_PATH ] || exit 5
    [ -f $CONF_FILE ] || exit 6
    echo -n $"Starting $prog: "
    daemon $DAEMON_PATH  > /var/log/node_exporter.log 2>&1 &
    retval=$?
    echo
    [ $retval -eq 0 ] && touch $LOCK_FILE
    return $retval
}

stop() {
    echo -n $"Stopping $prog: "
    killproc $prog -QUIT
    retval=$?
    echo
    [ $retval -eq 0 ] && rm -f $LOCK_FILE
    return $retval
}

restart() {
    stop
    start
}

reload() {
    echo -n $"Reloading $prog: "
    killproc $DAEMON_PATH -HUP
    RETVAL=$?
    echo
}

force_reload() {
    restart
}


rh_status() {
    status $prog
}

rh_status_q() {
    rh_status >/dev/null 2>&1
}

case "$1" in
    start)
        rh_status_q && exit 0
        $1
        ;;
    stop)
        rh_status_q || exit 0
        $1
        ;;
    restart)
        $1
        ;;
    reload)
        rh_status_q || exit 7
        $1
        ;;
    force-reload)
        force_reload
        ;;
    status)
        rh_status
        ;;
    condrestart|try-restart)
        rh_status_q || exit 0
            ;;
    *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart|try-restart|reload|force-reload}"
        exit 2
esac